<?php
require_once dirname(__FILE__) . "/../config/functions.php";
$fullUrl = full_url();
include_once dirname(__FILE__) . "/header.php";
?>

<?php
$sendFlag = false;
if (isset($_GET['id']) ) {
    $id = $_GET['id'];
    $_qry = "UPDATE `ads` SET `status` = 1 WHERE `id` ='" . $id . "'";
    mysql_query($_qry);
    $sendFlag = true;
}

?>
<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1>Sponsor Ads</h1>
                <?php if ($sendFlag) : ?>
                <div class="alert alert-success alert-dismissable">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    Sponsor Ad approved successfully.
                </div>
            <?php endif; ?>
            <ol class="breadcrumb">
                <?php
                //count Sponsors
                $activeSponsors = mysql_query("SELECT count(*) as totCount from ads WHERE level=0 AND status=1");
                $activeSponsorsCount = mysql_fetch_assoc($activeSponsors);
                $activeSponsorsCount = $activeSponsorsCount['totCount'];

                $pendingSponsors = mysql_query("SELECT count(*) as totCount from ads WHERE level=0 AND status=0");
                $pendingSponsorsCount = mysql_fetch_assoc($pendingSponsors);
                $pendingSponsorsCount = $pendingSponsorsCount['totCount'];
                ?>
                <li class="active">
                    <i class="fa fa-dashboard"></i> Filter Sponsor Ads
                    <br/>
                    <b>Active Sponsor Ads:</b> <?php echo $activeSponsorsCount; ?>&nbsp;&nbsp;&nbsp;<b>Pending Sponsor Ads:</b> <?php echo $pendingSponsorsCount; ?>
                </li>
            </ol>
            <form class="form-horizontal" role="form" method="get" action="<?php echo strtok($fullUrl, '?'); ?>">
                <div class="form-group">
                    <label for="inputOptions" class="col-sm-2 control-label">Options</label>
                    <div class="col-sm-5">
                        <select class="form-control" id="inputOptions" name="status">
                            <option value="">-- Sponsor Ad Status --</option>
                            <option value="true" <?php if (isset($_GET['status']) && $_GET['status'] == 'true'): ?>selected=""<?php endif; ?>>Active</option>
                            <option value="false" <?php if (isset($_GET['status']) && $_GET['status'] == 'false'): ?>selected=""<?php endif; ?>>Pending</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">Search</button>
                    </div>
                </div>
            </form>

            <?php
            $itemsPerPage = 5;
            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $ofsset = ($page - 1) * $itemsPerPage;
            $limit = "limit $ofsset,$itemsPerPage";

            $query = "SELECT * from ads WHERE level=0 AND status <> 2";


            //check for status filter
            if (isset($_GET['status']) && $_GET['status']) {
                $query .= " and status = " . mysql_real_escape_string($_GET['status']);
            }

            $sponsors = mysql_query("$query order by name asc $limit");
            //count all users
            $sponsorsCount = mysql_query("$query");
            $sponsorsCount = mysql_num_rows($sponsorsCount);

            //calculate the last page number
            $lastPageNumber = (int) ($sponsorsCount / $itemsPerPage);
            if (($sponsorsCount % $itemsPerPage) > 0) {
                $lastPageNumber++;
            }
            ?>
            <table class="table table-bordered">
                <thead><tr><th>Advertiser Info</th><th>Tracking Pass</th><th>Sponsor Ad</th><th>Hits Purchased</th><th>Remaining</th><th>Hits Received</th><th>Date</th><th>Status</th><th>Action</th></tr></thead>
                <tbody>
                    <?php
                    $index = 1;
                    while ($row = mysql_fetch_array($sponsors)) :
                        ?>
                        <tr>
                            <td>
                                <?php
                                if(!$row['assigned']){
                                    echo "Default Sponsor Ad";
                                }else{ ?>                                  
                                <b>Name: </b><?php echo $row['name']; ?><br>
                                <b>Email:</b> <?php echo $row['email']; ?><br>
                                <b>Payment Mode: </b><?php echo $row['payment_type']; ?>
                                <?php } ?>
                            </td>
                            <td><?php echo $row['tracking_password']; ?></td>
                            <td><a href="<?= $row['url'] ?>" target="_blank">
                                    <?= $row['textAd'] ?>
                                </a></td>
                            <td>
                                <?php
                                if(!$row['assigned']){
                                    echo "Default Sponsor Ad";
                                }else{
                                    echo $row['assigned']; 
                                }
                                ?>
                            </td>
                            <td>
                                <?php
                                if(!$row['assigned']){
                                    echo "0";
                                }else{
                                   echo  $row['assigned'] - $row['current']; 
                                }
                                ?>
                            </td>
                            <td><?php echo $row['clicks']; ?></td>
                            <td><?php echo date("Y-m-d", strtotime($row['created_at'])); ?></td>
                            <td>
                                <?php
                                if ($row['status'] == 1) {
                                    echo 'Active';
                                } elseif ($row['status'] == 0) {
                                    echo 'Pending';
                                }
                                ?>
                            </td>
                            <td>
                                <?php
                                if ($row['status'] == 0) { ?>
                                <a title="Approve" href="sponsors.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-ok"></span></a><br>
                                  <?php  
                                } ?>
                                
                                <a title="Edit" href="sponsor-edit.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-pencil"></span></a><br>
                                <a title="Remove" href="sponsor-delete-confirm.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-remove"></span></a>                                
                            </td>
                        </tr>
                        <?php
                        $index++;
                    endwhile;
                    ?>
                </tbody>
            </table>

            <?php
            if ($lastPageNumber > 1):
                $fullUrl = strtok($fullUrl, '?');
                ?>
                <ul class="pagination">
                    <?php if ($page > 1): ?>
                        <li>
                            <a href="<?php
                $params = $_GET;
                $params['page'] = $page - 1;
                $paramString = http_build_query($params);
                echo $fullUrl . '?' . $paramString;
                        ?>">«</a>
                        </li>
                    <?php endif; ?>
                    <?php for ($index = 1; $index <= $lastPageNumber; $index++): ?>
                        <?php if ($index > $page - 3 && $index < $page + 3): ?>
                            <li <?php if ($index == $page): ?>class="active"<?php endif; ?>>
                                <a href="<?php
                $params = $_GET;
                $params['page'] = $index;
                $paramString = http_build_query($params);
                echo $fullUrl . '?' . $paramString;
                            ?>"><?php echo $index; ?></a>
                            </li>
                        <?php endif; ?>
                    <?php endfor; ?>    
                    <?php if ($page < $lastPageNumber): ?>
                        <li>
                            <a href="<?php
                $params = $_GET;
                $params['page'] = $page + 1;
                $paramString = http_build_query($params);
                echo $fullUrl . '?' . $paramString;
                        ?>">»</a>
                        </li>
                    <?php endif; ?>
                </ul>
            <?php endif; ?>

        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->

<?php include_once dirname(__FILE__) . "/footer.php"; ?>